package com.yanqu.road.dao.impl.activity.suitang.data;

import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.suitang.data.CrossSuiTangUnionBuGaoData;
import com.alibaba.fastjson.JSON;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.entity.activity.suitang.enums.eSuiTangWarType;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

public class CrossSuiTangUnionBuGaoDataDaoImpl extends TempDataObjectDao<CrossSuiTangUnionBuGaoData> {
    public static String getCrossSuiTangUnionBuGaoDataSql = "select * from `t_c_suitang_union_bugao_data` where `activityId`=? and `groupId`=?";
    public Map<String, Map<Long, CrossSuiTangUnionBuGaoData>> getCrossSuiTangUnionBuGaoData(int activityId,int groupId) {
        Map<String, Map<Long, CrossSuiTangUnionBuGaoData>> buGaoMap = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getCrossSuiTangUnionBuGaoDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setInt(i++, groupId);
            rs = call.executeQuery();
            while (rs.next()) {
                CrossSuiTangUnionBuGaoData buGaoData = parse(rs);
                Map<Long, CrossSuiTangUnionBuGaoData> unionBuGaoDataMap = buGaoMap.get(buGaoData.getUnionUid());
                if (unionBuGaoDataMap == null) {
                    unionBuGaoDataMap = new ConcurrentHashMap<>();
                    buGaoMap.put(buGaoData.getUnionUid(), unionBuGaoDataMap);
                }
                unionBuGaoDataMap.put(buGaoData.getBuGaoNo(), buGaoData);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return buGaoMap;
    }

    public static String updateCrossSuiTangUnionBuGaoDataSql = "update `t_c_suitang_union_bugao_data` set actGroupId=?,round=?,warType=?,userId=?,goodId=?,time=?,getRewardUserList=? where activityId=? and groupId=? and unionUid=? and buGaoNo=?";
    @Override
    protected boolean updateData(CrossSuiTangUnionBuGaoData crossSuiTangUnionBuGaoData) {
        crossSuiTangUnionBuGaoData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, crossSuiTangUnionBuGaoData.getActGroupId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossSuiTangUnionBuGaoData.getRound()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossSuiTangUnionBuGaoData.getWarType())));
        para.put(i++, new DbParameter(Types.BIGINT, crossSuiTangUnionBuGaoData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossSuiTangUnionBuGaoData.getGoodId()));
        para.put(i++, new DbParameter(Types.BIGINT, crossSuiTangUnionBuGaoData.getTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossSuiTangUnionBuGaoData.getGetRewardUserList())));
        para.put(i++, new DbParameter(Types.INTEGER, crossSuiTangUnionBuGaoData.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossSuiTangUnionBuGaoData.getGroupId()));
        para.put(i++, new DbParameter(Types.VARCHAR, crossSuiTangUnionBuGaoData.getUnionUid()));
        para.put(i++, new DbParameter(Types.BIGINT, crossSuiTangUnionBuGaoData.getBuGaoNo()));
        if (getDb().executeNoneQuery(updateCrossSuiTangUnionBuGaoDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertCrossSuiTangUnionBuGaoDataSql = "insert into `t_c_suitang_union_bugao_data`(activityId,groupId,unionUid,actGroupId,round,warType,userId,buGaoNo,goodId,time,getRewardUserList) values (?,?,?,?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(CrossSuiTangUnionBuGaoData crossSuiTangUnionBuGaoData) {
        crossSuiTangUnionBuGaoData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, crossSuiTangUnionBuGaoData.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossSuiTangUnionBuGaoData.getGroupId()));
        para.put(i++, new DbParameter(Types.VARCHAR, crossSuiTangUnionBuGaoData.getUnionUid()));
        para.put(i++, new DbParameter(Types.INTEGER, crossSuiTangUnionBuGaoData.getActGroupId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossSuiTangUnionBuGaoData.getRound()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossSuiTangUnionBuGaoData.getWarType())));
        para.put(i++, new DbParameter(Types.BIGINT, crossSuiTangUnionBuGaoData.getUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, crossSuiTangUnionBuGaoData.getBuGaoNo()));
        para.put(i++, new DbParameter(Types.INTEGER, crossSuiTangUnionBuGaoData.getGoodId()));
        para.put(i++, new DbParameter(Types.BIGINT, crossSuiTangUnionBuGaoData.getTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossSuiTangUnionBuGaoData.getGetRewardUserList())));
        if (getDb().executeNoneQuery(insertCrossSuiTangUnionBuGaoDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected CrossSuiTangUnionBuGaoData parseData(ResultSet rs) throws SQLException {
        CrossSuiTangUnionBuGaoData crossSuiTangUnionBuGaoData = new CrossSuiTangUnionBuGaoData();
        crossSuiTangUnionBuGaoData.setActivityId(rs.getInt("activityId"));
        crossSuiTangUnionBuGaoData.setGroupId(rs.getInt("groupId"));
        crossSuiTangUnionBuGaoData.setUnionUid(rs.getString("unionUid"));
        crossSuiTangUnionBuGaoData.setActGroupId(rs.getInt("actGroupId"));
        crossSuiTangUnionBuGaoData.setRound(rs.getInt("round"));
        crossSuiTangUnionBuGaoData.setWarType(JSON.parseObject(rs.getString("warType"), eSuiTangWarType.class));
        crossSuiTangUnionBuGaoData.setUserId(rs.getLong("userId"));
        crossSuiTangUnionBuGaoData.setBuGaoNo(rs.getLong("buGaoNo"));
        crossSuiTangUnionBuGaoData.setGoodId(rs.getInt("goodId"));
        crossSuiTangUnionBuGaoData.setTime(rs.getLong("time"));
        crossSuiTangUnionBuGaoData.setGetRewardUserList(JSON.parseObject(rs.getString("getRewardUserList"), Set.class));

        return crossSuiTangUnionBuGaoData;
    }
}
